package com.itheima.demo05;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/7/13 19:02
 */
public class Foodli implements Runnable {
    private Desk desk = new Desk();

    public Foodli(Desk desk) {
        this.desk = desk;
    }

    //厨师类;
    @Override
    public void run() {
        //先判断有没有包子;
        while (true) {
            synchronized (desk.getLock()) {
                if (desk.getCount() == 0) {
                    break;
                }
                if (!desk.isFlag()) {
                    System.out.println("厨师做了一个包子!");
                    //变更flag状态;
                    desk.setFlag(true);
                    //唤醒等待线程;
                    desk.getLock().notifyAll();
                } else {
                    //说明有包子,厨师等待;
                    try {
                        desk.getLock().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }
}
